package com.tryee.wx.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.tryee.wx.config.utils.WxUtils;
import com.tryee.wx.domain.User;
import com.tryee.wx.pojo.UserMsgVo;
import com.tryee.wx.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.tryee.common.annotation.Log;
import com.tryee.common.enums.BusinessType;
import com.tryee.wx.domain.UserMsg;
import com.tryee.wx.service.IUserMsgService;
import com.tryee.framework.web.base.BaseController;
import com.tryee.common.page.TableDataInfo;
import com.tryee.common.base.AjaxResult;
import com.tryee.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletRequest;

/**
 * 消息管理
 * 信息操作处理
 *
 * @author tryee
 * @date 2019-04-03
 */
@Controller
@RequestMapping("/wx/userMsg")
public class UserMsgController extends BaseController {
    private String prefix = "wx/userMsg";

    @Autowired
    private IUserMsgService userMsgService;

    @Autowired
    private WxUtils wxUtils;



    @RequiresPermissions("wx:userMsg:view")
    @GetMapping()
    public String userMsg() {
        return prefix + "/userMsg";
    }

    /**
     * 查询消息管理
     * 列表
     */
    @RequiresPermissions("wx:userMsg:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserMsgVo userMsg) {
        startPage();
        List<UserMsgVo> list = userMsgService.selectUserMsgList(userMsg);
        return getDataTable(list);
    }


    /**
     * 导出消息管理
     * 列表
     */
    @RequiresPermissions("wx:userMsg:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserMsgVo userMsg) {
        List<UserMsgVo> list = userMsgService.selectUserMsgList(userMsg);
        ExcelUtil<UserMsgVo> util = new ExcelUtil<UserMsgVo>(UserMsgVo.class);
        return util.exportExcel(list, "userMsg");
    }

    /**
     * 新增消息管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存消息管理
     */
    @RequiresPermissions("wx:userMsg:add")
    @Log(title = "消息管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserMsg userMsg) {
        return toAjax(userMsgService.insertUserMsg(userMsg));
    }

    /**
     * 修改消息管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        UserMsg userMsg = userMsgService.selectUserMsgById(id);
        mmap.put("userMsg", userMsg);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息管理
     */
    @RequiresPermissions("wx:userMsg:edit")
    @Log(title = "消息管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserMsg userMsg) {
        return toAjax(userMsgService.updateUserMsg(userMsg));
    }

    /**
     * 删除消息管理
     */
    @RequiresPermissions("wx:userMsg:remove")
    @Log(title = "消息管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(userMsgService.deleteUserMsgByIds(ids));
    }


    /**
     * 跳转回复内容页面
     */
    @GetMapping("reply")
    public String replay(String openid, String nickname,ModelMap mmp){
        mmp.put("openid", openid);
        mmp.put("nickname", nickname);
        return prefix+"/reply";
    }

    /**
     * 发送回复内容
     */
    @PostMapping("toReply")
    @ResponseBody
    public AjaxResult toReply(String openid,String content){
        int result = wxUtils.replyMsg(openid, content);
        if (result == 0) {
            return success();
        }

        return error("发送失败！请检查用户是否已经取消关注！");
    }
}
